#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os

import requests


def getJD():
    url = 'https://item.jd.com/100011336110.html'
    try:
        r = requests.get(url)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except:
        print('爬取失败')


def getAWS():
    url = 'https://www.amazon.cn/dp/B07TGY52SJ/ref=lp_1535691071_1_1?s=shoes&ie=UTF8&qid=1587997926&sr=1-1'
    try:
        kv = {'User-Agent': 'Mozilla/5.0'}
        r = requests.get(url, headers=kv)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except:
        print("爬取失败")


def getBaidu():
    keyword = 'Python'
    h = {'User-Agent': 'Mozilla/5.0'}
    kv = {'wd': keyword}
    r = requests.get("http://www.baidu.com/s", headers=h, params=kv)
    print(r.request.url)
    try:
        r.raise_for_status()
        print(len(r.text))
        r.encoding = r.apparent_encoding
        print(r.text)
    except:
        print('爬取失败')


def get360():
    keyword = 'Python'
    kv = {'q': keyword}
    r = requests.get("http://www.so.com/s", params=kv)
    print(r.request.url)
    try:
        r.raise_for_status()
        print(len(r.text))
        r.encoding = r.apparent_encoding
        print(r.text)
    except:
        print('爬取失败')


def getImage():
    url = 'https://cdn.jsdelivr.net/gh/eternidad33/picbed@master/img/dva319.jpg'
    root = "F://pics//"
    path = root + url.split('/')[-1]
    try:
        if not os.path.exists(root):
            os.mkdir(root)
        if not os.path.exists(path):
            r = requests.get(url)
            with open(path, 'wb') as f:
                f.write(r.content)
                f.close()
                print('文件保存成功')
        else:
            print('文件已存在')
    except:
        print('爬取失败')


def getIP():
    url = 'https://www.ip138.com/'
    try:
        r = requests.get(url)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        print(r.text[-500:])
    except:
        print('爬取失败')


if __name__ == '__main__':
    # print(getJD())
    # print(getAWS())
    # getBaidu()
    # get360()
    # getImage()
    getIP()
